#include "filter_index.h"
FilterIndex::FilterIndex(){

}
void FilterIndex::addIntFieldFilter(const std::string& fieldName, int64_t value, uint64_t id){
    roaring_bitmap_t* bitmap = roaring_bitmap_create();
    roaring_bitmap_add(bitmap, id);
    intFieldFilter[fieldName][value]=bitmap;
    GlobalLogger->info("addIntFieldFilter: fieldName={}, value={}, id={}", fieldName, value, id);
}
void FilterIndex::updateIntFieldFilter(const std::string& fieldName, int64_t* old_value,int64_t new_value, uint64_t id){
    // 检查是否存在旧值
    if(old_value!=nullptr){
        GlobalLogger->info("updateIntFieldFilter: fieldName={}, old_value={}, new_value={}, id={}", fieldName, *old_value, new_value, id);
    }else{
        GlobalLogger->info("updateIntFieldFilter: fieldName={}, old_value=nullptr, new_value={}, id={}", fieldName, new_value, id);
    }
    // 先检查是否存在该字段
    auto it=intFieldFilter.find(fieldName);
    // 不存在则创建
    if(it==intFieldFilter.end()){
        addIntFieldFilter(fieldName, new_value, id);
    }else{
        // 存在则更新
        std::map<int64_t, roaring_bitmap_t*>& value_map=it->second;
        auto old_bitmap_it=(old_value!=nullptr)?value_map.find(*old_value):value_map.end();
        if(old_bitmap_it!=value_map.end()){
            roaring_bitmap_t* old_bitmap=old_bitmap_it->second;
            roaring_bitmap_remove(old_bitmap, id);
        }

        //查找新值对应的位图，不存在则新创建
        auto new_bitmap_it=value_map.find(new_value);
        if(new_bitmap_it==value_map.end()){
            roaring_bitmap_t* new_bitmap=roaring_bitmap_create();
            value_map[new_value]=new_bitmap;
            new_bitmap_it=value_map.find(new_value);
        }

        roaring_bitmap_t* new_bitmap=new_bitmap_it->second;
        roaring_bitmap_add(new_bitmap, id);
    }
}
void FilterIndex::getIntFieldFilter(const std::string& fieldName, int64_t value, Operation op, roaring_bitmap_t* result_bitmap){
    auto it=intFieldFilter.find(fieldName);
    if(it!=intFieldFilter.end()){
        auto& value_map=it->second;
        if(op==Operation::EQUAL){
            auto bitmap_it=value_map.find(value);
            if(bitmap_it!=value_map.end()){
                GlobalLogger->info("Retrieved EQUAL bitmap for fieldName={}, value={}", fieldName, value);
                roaring_bitmap_or_inplace(result_bitmap, bitmap_it->second);
            }
        }else if(op==Operation::NOT_EQUAL){ 
            for(const auto& pair:value_map){
                if(pair.first!=value){
                    roaring_bitmap_or_inplace(result_bitmap, pair.second);
                }
            }
            GlobalLogger->info("Retrieved NOT_EQUAL bitmap for fieldName={}, value={}", fieldName, value);
        }
    }
}

//序列化
std::string FilterIndex::serializeIntFieldFilter() const{
    std::ostringstream oss;
    for(const auto& entry:intFieldFilter){
        const std::string& fieldName = entry.first;
        const std::map<int64_t, roaring_bitmap_t*>& value_map = entry.second;

        for(const auto& value_entry:value_map){
            long value = value_entry.first;
            const roaring_bitmap_t* bitmap = value_entry.second;

            //序列化位图
            uint32_t size=roaring_bitmap_portable_size_in_bytes(bitmap);
            char* serialized_bitmap=new char[size];
            roaring_bitmap_portable_serialize(bitmap,serialized_bitmap);

            //写入输出流
            oss<<fieldName<<"|"<<value<<"|";
            oss.write(serialized_bitmap,size);
            oss<<std::endl;

            delete[] serialized_bitmap;
        }
    }

    return oss.str();
}
    //反序列化
void FilterIndex::deserializeIntFieldFilter(const std::string& serialized_data){
    std::istringstream iss(serialized_data);

    std::string line;
    while(getline(iss,line)){
        std::istringstream line_iss(line);

        std::string fieldName,value_str;
        getline(line_iss,fieldName,'|');
        getline(line_iss,value_str,'|');
        long value=std::stol(value_str);

        //读取序列化位图
        std::string bitmap_str(std::istream_iterator<char>(line_iss),{});
        //反序列化
        roaring_bitmap_t* bitmap=roaring_bitmap_portable_deserialize(bitmap_str.data());

        intFieldFilter[fieldName][value]=bitmap;
    }
}
    //保存
    void FilterIndex::saveIndex(ScalarStorage& scalarStorage,const std::string&key){
        std::string serialized_data=serializeIntFieldFilter();
        scalarStorage.put(key,serialized_data);
    }
    //加载
    void FilterIndex::loadIndex(ScalarStorage& scalarStorage,const std::string&key){
        std::string deserialized_data=scalarStorage.get(key);
        deserializeIntFieldFilter(deserialized_data);
    }